﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

/* Generated  */
namespace EDMXParserV1
{
    [Serializable]
    public class Edmx
    {
        // base types
        public String Version { get; set; }
        public String xmlns_edmx { get; set; }

        // class types
        public Runtime Runtime { get; set; }


        public Edmx(String FileName)
        {
            String Text = System.IO.File.ReadAllText(FileName);
            XmlDocument Doc = new XmlDocument();
            Doc.LoadXml(Text);

            XmlElement XMLEdmx = Doc.DocumentElement;
            Load(XMLEdmx);
            LinkConnections();
        }

        private void Load(XmlElement XmlElement)
        {
            foreach (XmlAttribute Attribute in XmlElement.Attributes)
            {
                switch (Attribute.Name)
                {
                    case "Version":
                        Version = Attribute.Value;
                        break;
                    case "xmlns:edmx":
                        xmlns_edmx = Attribute.Value;
                        break;
                    default:
                        Globals.AttributeSwitchCoder(Attribute);
                        break;
                }
            }

            foreach (XmlNode ChildNode in XmlElement.ChildNodes)
            {
                switch (ChildNode.Name)
                {
                    case "#comment":
                        break;

                    case "edmx:Runtime":
                        Runtime = Runtime.New(ChildNode);
                        break;
                    case "Designer":
                        break;
                    default:
                        Globals.NodeSwitchCoder(ChildNode);
                        break;
                }
            }

        }

        private void LinkConnections()
        {

            String SchemaNamespace = Runtime.ConceptualModels.Schema.Namespace;
            foreach (EntitySet EntitySet in Runtime.ConceptualModels.Schema.EntityContainer.EntitySets)
            {
                foreach (EntityType EntityType in Runtime.ConceptualModels.Schema.EntityTypes)
                {
                    String SchemaNamespaceEntityTypeName = SchemaNamespace + "." + EntityType.Name;
                    String SchemaAliasEntityTypeName = Runtime.ConceptualModels.Schema.Alias + "." + EntityType.Name;

                    if (EntitySet.EntityType == SchemaNamespaceEntityTypeName)
                    {
                        EntitySet.EntityTypeLink = EntityType;
                        break;
                    }
                    if (EntitySet.EntityType == SchemaAliasEntityTypeName)
                    {
                        EntitySet.EntityTypeLink = EntityType;
                        break;
                    }
                }
            }

            foreach (EntitySet EntitySet in Runtime.StorageModels.Schema.EntityContainer.EntitySets)
            {
                foreach (EntityType EntityType in Runtime.ConceptualModels.Schema.EntityTypes)
                {
                    String SchemaAliasEntityTypeName = Runtime.StorageModels.Schema.Alias + "." + EntityType.Name;
                    String SchemaNamespaceEntityTypeName = SchemaNamespace + "." + EntityType.Name;

                    if (EntitySet.EntityType == SchemaAliasEntityTypeName)
                    {
                        EntitySet.EntityTypeLink = EntityType;
                        break;
                    }

                    if (EntitySet.EntityType == SchemaNamespaceEntityTypeName)
                    {
                        EntitySet.EntityTypeLink = EntityType;
                        break;
                    }
                }
            }

            foreach (EntityType EntityType in Runtime.ConceptualModels.Schema.EntityTypes)
            {
                foreach (NavigationProperty NavigationProperty in EntityType.NavigationProperties)
                {
                    if (NavigationProperty.Name == "Supplier")
                    {
                        NavigationProperty.Name = NavigationProperty.Name;
                    }
                    foreach (Association Association in Runtime.ConceptualModels.Schema.Associations)
                    {
                        if (SchemaNamespace + "." + Association.Name == NavigationProperty.Relationship)
                        {
                            NavigationProperty.Association = Association;
                            break;
                        }

                        if (Runtime.ConceptualModels.Schema.Alias + "." + Association.Name == NavigationProperty.Relationship)
                        {
                            NavigationProperty.Association = Association;
                            break;
                        }
                    }

                    EntitySet FromRoleCurrentEntitySet = null;
                    EntitySet ToRoleCurrentEntitySet = null;

                    foreach (EntitySet ES in Runtime.ConceptualModels.Schema.EntityContainer.EntitySets)
                    {
                        if (NavigationProperty.Association != null)
                        {
                            if (NavigationProperty.Association.ReferentialConstraint != null)
                            {
                                if (ES.EntityTypeLink != null)
                                {
                                    if (ES.Name == NavigationProperty.Association.ReferentialConstraint.Dependent.Role)
                                    {
                                        FromRoleCurrentEntitySet = ES;
                                    }

                                    if (ES.Name == NavigationProperty.Association.ReferentialConstraint.Principal.Role)
                                    {
                                        ToRoleCurrentEntitySet = ES;
                                    }
                                }
                            }
                        }
                    }



                    if (FromRoleCurrentEntitySet != null)
                    {
                        NavigationProperty.FromEntityType = FromRoleCurrentEntitySet.EntityTypeLink;
                    }
                    else
                    {
                        foreach (EntitySet ES in Runtime.ConceptualModels.Schema.EntityContainer.EntitySets)
                        {
                            if (NavigationProperty.Association != null)
                            {
                                if (NavigationProperty.Association.ReferentialConstraint != null)
                                {
                                    if (ES.EntityTypeLink != null)
                                    {
                                        if (ES.EntityTypeLink.Name == NavigationProperty.Association.ReferentialConstraint.Dependent.Role)
                                        {
                                            FromRoleCurrentEntitySet = ES;
                                        }
                                    }
                                }
                            }
                        }

                        if (FromRoleCurrentEntitySet != null)
                        {
                            NavigationProperty.FromEntityType = FromRoleCurrentEntitySet.EntityTypeLink;
                        }
                        else
                        {
                            Debug.WriteLine("Not good.");
                        }

                    }

                    if (ToRoleCurrentEntitySet != null)
                    {
                        NavigationProperty.ToEntityType = ToRoleCurrentEntitySet.EntityTypeLink;
                    }
                    else
                    {
                        foreach (EntitySet ES in Runtime.ConceptualModels.Schema.EntityContainer.EntitySets)
                        {
                            if (NavigationProperty.Association != null)
                            {
                                if (NavigationProperty.Association.ReferentialConstraint != null)
                                {
                                    if (ES.EntityTypeLink != null)
                                    {
                                        if (ES.Name == NavigationProperty.Association.ReferentialConstraint.Principal.Role)
                                        {
                                            ToRoleCurrentEntitySet = ES;
                                        }
                                    }
                                }
                            }
                        }

                        if (ToRoleCurrentEntitySet != null)
                        {
                            NavigationProperty.ToEntityType = ToRoleCurrentEntitySet.EntityTypeLink;
                        }
                        else
                        {
                            Debug.WriteLine("Not good1");
                        }

                    }


                    //foreach (EntityType ET in Runtime.ConceptualModels.Schema.EntityTypes)
                    //{
                    //    if (ET.Name == CurrentEntitySet.EntityTypeLink.Name )
                    //    {
                    //        NavigationProperty.FromEntityType = ET;
                    //    }

                    //    if (ET.Name == NavigationProperty.FromRole)
                    //    {
                    //        NavigationProperty.FromEntityType = ET;
                    //    }


                    //}

                    foreach (EntityType ET in Runtime.ConceptualModels.Schema.EntityTypes)
                    {
                        if (ET.Name == NavigationProperty.ToRole)
                        {
                            NavigationProperty.ToEntityType = ET;
                            break;
                        }
                    }

                    //NavigationProperty.FromRole 
                    //NavigationProperty.ToRole



                }
            }

            foreach (EntityType EntityType in Runtime.ConceptualModels.Schema.EntityTypes)
            {
                foreach (NavigationProperty NavigationProperty in EntityType.NavigationProperties)
                {
                    //foreach (Association Association in Runtime.ConceptualModels.Schema.Associations)
                    //{
                    //    if (SchemaNamespace + "." + Association.Name == NavigationProperty.Relationship)
                    //    {
                    //        NavigationProperty.Association = Association;
                    //    }
                    //}

                    foreach (AssociationSet CurrentAssociationSet in Runtime.ConceptualModels.Schema.EntityContainer.AssociationSets)
                    {
                        if (CurrentAssociationSet.Association == NavigationProperty.Relationship)
                        {
                            NavigationProperty.AssociationSet = CurrentAssociationSet;

                            foreach (Association CurrentAssociation in Runtime.ConceptualModels.Schema.Associations)
                            {
                                if (CurrentAssociation.Name == CurrentAssociationSet.Name)
                                {
                                    NavigationProperty.Association = CurrentAssociation;


                                }
                            }

                            break;
                        }
                    }

                }
            }

            foreach (Association CurrentAssociation in Runtime.ConceptualModels.Schema.Associations)
            {
                foreach (AssociationEnd CurrentAssociationEnd in CurrentAssociation.AssociationEnds)
                {
                    foreach (EntityType EntityType in Runtime.ConceptualModels.Schema.EntityTypes)
                    {

                        if (SchemaNamespace + "." + EntityType.Name == CurrentAssociationEnd.Type)
                        {
                            CurrentAssociationEnd.EntityType = EntityType;
                            break;
                        }
                        if (Runtime.ConceptualModels.Schema.Alias + "." + EntityType.Name == CurrentAssociationEnd.Type)
                        {
                            CurrentAssociationEnd.EntityType = EntityType;
                            break;
                        }
                    }
                }
            }

            //dan4  

            //Dictionary<String, String> LookupTables = new Dictionary<string, string>();

            //foreach (Association CurrentAssociation in Runtime.StorageModels.Schema.Associations)
            //{
            //    if (IsAssociationValidToUseForLookupTable(CurrentAssociation))
            //    {
            //        LookupTablesKeys = CurrentAssociation.ReferentialConstraint.Principal.Role 
            //    }
            //    else
            //    {
            //        // do nothing
            //    }
            //}
        }

        //private bool IsAssociationValidToUseForLookupTable( Association CurrentAssociation )
        //{
        //    bool IsAssociationValid = false;

        //    // Valid association
        //    if ( CurrentAssociation.IsNotNull() )
        //    {
        //        // Correct number of Endpoints.
        //        if (CurrentAssociation.AssociationEnds.Count() == 2)
        //        {
        //            // Valid Referential Constraint section.
        //            if ( CurrentAssociation.ReferentialConstraint.IsNotNull() )
        //            {
        //                // Valid Dependent object.
        //                if ( CurrentAssociation.ReferentialConstraint.Dependent.IsNotNull() )
        //                {
        //                    // Valid Principal object.
        //                    if ( CurrentAssociation.ReferentialConstraint.Principal.IsNotNull() )
        //                    {
        //                        IsAssociationValid = true;
        //                    }
        //                }
        //            }
        //        }
        //    }

        //    return IsAssociationValid;
        //}

    } // /public class EDMX
}
